# Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.

# CMake lowest version requirement
cmake_minimum_required(VERSION 3.5.1)

# project information
project(object_detection_and_track)

# Compile options
add_compile_options(-std=c++17)

# output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  "../../../out")

# debug and release
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC -O0 -g -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -O2 -Wall")

# openmp support
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS"${CMAKE_EXE_LINKER_FLAGS}${OpenMP_EXE_LINKER_FLAGS}")
endif()

add_definitions(-DENABLE_DVPP_INTERFACE)

set(INC_PATH $ENV{DDK_PATH})

if (NOT DEFINED ENV{DDK_PATH})
    # set(INC_PATH "$ENV{HOME}/Ascend/ascend-toolkit/latest/arm64-linux")
    set(INC_PATH "/usr/local/Ascend")
    message(STATUS "set default INC_PATH: ${INC_PATH}")
else ()
    message(STATUS "env INC_PATH: ${INC_PATH}")
endif()

set(LIB_PATH $ENV{DDK_PATH}/acllib/lib64/stub)

if (NOT DEFINED ENV{NPU_HOST_LIB})
    # set(LIB_PATH "$ENV{HOME}/Ascend/ascend-toolkit/latest/arm64-linux/acllib/lib64/stub/")
    set(LIB_PATH "/usr/local/Ascend/acllib/lib64/stub/")
    message(STATUS "set default LIB_PATH: ${LIB_PATH}")
else ()
    message(STATUS "env LIB_PATH: ${LIB_PATH}")
endif()


list(APPEND COMMON_DEPEND_LIB avcodec avformat avdevice avutil swresample avfilter swscale)

if (${CMAKE_CXX_COMPILER} MATCHES "aarch")
    set(ARCH "arm")
    list(APPEND COMMON_DEPEND_LIB media_mini)
    add_definitions(-DENABLE_BOARD_CAMARE)
else ()
    set(ARCH "x86")
    string(REGEX REPLACE "common/src/camera.cpp" "" COMMON_SRC "${COMMON_SRC_ALL}")
endif()

aux_source_directory(retina/src RETINA_SRC)
aux_source_directory(yolo/src YOLO_SRC)
aux_source_directory(deepsort/src DEEPSORT_SRC)
aux_source_directory(dimp/src DIMP_SRC)
aux_source_directory(common/src COMMON_SRC)

# Header path
include_directories(
    ../inc/
    common/inc/
    yolo/inc/
    deepsort/inc/
    retina/inc/
    dimp/inc/
    $ENV{HOME}/ascend_ddk/${ARCH}/include/
    $ENV{HOME}/ascend_ddk/${ARCH}/include/ascenddk
    ${INC_PATH}/acllib/include/
    $ENV{HOME}/Ascend/driver
    $ENV{HOME}/ascend_ddk/${ARCH}/include/opencv4

)

if(target STREQUAL "Simulator_Function")
    add_compile_options(-DFUNC_SIM)
endif()

# add host lib path
link_directories(
    /usr/local/lib/
    ${LIB_PATH}
    $ENV{HOME}/ascend_ddk/${ARCH}/lib/
    ${INC_PATH}/atc/lib64
    ${INC_PATH}/aclib/lib64/stub/
    $ENV{HOME}/Ascend/
    $ENV{HOME}/Ascend/driver/
)

add_executable(app
    ${COMMON_SRC}
    ${YOLO_SRC}
    ${DEEPSORT_SRC}
    ${RETINA_SRC}
    ${DIMP_SRC}
    utils.cpp
    do_process.cpp
    target_storage.cpp
    target_sender.cpp
    main.cpp)



if(target STREQUAL "Simulator_Function")
    target_link_libraries(app funcsim)
else()
    target_link_libraries(app 
    opencv_calib3d opencv_features2d opencv_flann opencv_imgproc opencv_imgcodecs opencv_videoio opencv_video opencv_highgui opencv_core
    ascendcl acl_dvpp 
    stdc++ pthread dl stdc++fs
    ${COMMON_DEPEND_LIB})
endif()

install(TARGETS app DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
